package com.example.jizhangben;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.Toast;

import com.example.jizhangben.databinding.ActivityHomeBinding;

import java.text.SimpleDateFormat;
import java.util.Date;

import fragment.WeiXinFragment;
import fragment.ZhiFuBaoFragment;

public class HomeActivity extends AppCompatActivity {
private ActivityHomeBinding binding;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityHomeBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        WeiXinFragment weiXinFragment = new WeiXinFragment();
        ZhiFuBaoFragment zhiFuBaoFragment = new ZhiFuBaoFragment();
        SharedPreferences sp = getSharedPreferences("xinxi", Context.MODE_PRIVATE);
        binding.idName.setText(sp.getString("name","姓名错误"));
        @SuppressLint("SimpleDateFormat")
        String time =new SimpleDateFormat("yyyy年MM月dd日").format(new Date());
        binding.idTime.setText(time);
        getSupportFragmentManager().beginTransaction().add(R.id.fragment,weiXinFragment).commitAllowingStateLoss();

        binding.weixin.setOnClickListener(v -> {
            getSupportFragmentManager().beginTransaction().replace(R.id.fragment,weiXinFragment).commitAllowingStateLoss();
        });
        binding.zhifubao.setOnClickListener(v -> {
            getSupportFragmentManager().beginTransaction().replace(R.id.fragment,zhiFuBaoFragment).commitAllowingStateLoss();
        });

        binding.idTuichu.setOnClickListener(v -> {
            SharedPreferences.Editor editor = sp.edit();
            editor.putBoolean("ye",true);
            editor.commit();
            Intent intent = new Intent(HomeActivity.this,MainActivity.class);
            startActivity(intent);
            finish();
            Toast.makeText(getApplicationContext(), "退出成功", Toast.LENGTH_SHORT).show();
        });
    }
}